package com.whpost.service.wx.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.dao.t.TRegionDao;
import com.whpost.service.dao.t.TXzjgDao;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TRegion;
import com.whpost.service.entity.t.TRole;
import com.whpost.service.entity.t.TXzjg;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.WxBaseServices;
import com.whpost.service.service.t.TApiNonceService;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.tools.Url;
import com.whpost.service.wx.dao.*;
import com.whpost.service.wx.entiy.*;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by IntelliJ IDEA.
 *
 * @Author: Yin Zhuoyi
 * @Date: 2021/12/15 11:16
 */
@Service
public class CyxxServices extends WxBaseServices<WxCyxxMapper, WxCyxx> {

    @Resource
    private TApiNonceService tApiNonceService;
    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        try{
            JSONObject jsonObject = getData(map,loginId);
            if (StringUtils.isEmpty(loginId)){
                return new ResultInfo(500,"用户信息丢失！",null);
            }else{
                String curpage = "",pagesize="",cxmc="",state="",dlmc="";
                if (jsonObject.containsKey("curpage")) {
                    curpage = jsonObject.getString("curpage");
                }
                if (jsonObject.containsKey("pagesize")) {
                    pagesize = jsonObject.getString("pagesize");
                }
                if (jsonObject.containsKey("cxmc")) {
                    cxmc = jsonObject.getString("cxmc");
                }
                if (jsonObject.containsKey("state")) {
                    state = jsonObject.getString("state");
                }
                if (jsonObject.containsKey("dlmc")) {
                    dlmc = jsonObject.getString("dlmc");
                }

                if (StringUtils.isEmpty(curpage)) curpage = "1";
                if (StringUtils.isEmpty(pagesize)) pagesize = "30";

                QueryWrapper<WxCyxx> queryWrapper = new QueryWrapper<>();
                Page<WxCyxx> page = new Page<>( Integer.parseInt(curpage),  Integer.parseInt(pagesize));

                ListInfo<WxCyxx> wxCyxxListInfo = new ListInfo<>();
                if (!StringUtils.isEmpty(cxmc))
                {
                    queryWrapper.like("xm",cxmc);
                }
                queryWrapper.eq("hyxx_lsh",loginId);

                queryWrapper.orderByDesc("id");
                IPage<WxCyxx> wxCyxxIPage = getBaseMapper().selectPage(page,queryWrapper);

                PageInfo pageInfo = new PageInfo();
                pageInfo.setTotal(wxCyxxIPage.getTotal());
                pageInfo.setCurPage(wxCyxxIPage.getCurrent());
                pageInfo.setPageSize(wxCyxxIPage.getSize());
                pageInfo.setPageTotal(wxCyxxIPage.getPages());

                wxCyxxListInfo.setPageInfo(pageInfo);
                wxCyxxListInfo.setListInfo(wxCyxxIPage.getRecords());
                wxCyxxListInfo.setUserId(loginId);

                //查询条件的值
                Map<String,Object> cxtjMap = new HashMap<>();
                cxtjMap.put("cxmc",cxmc);

                //返回前端的内容
                Map<String,Object> rtnMap = new HashMap<>();
                rtnMap.put("cxjg",wxCyxxListInfo);
                rtnMap.put("cxtj",cxtjMap);

                ResultInfo resultInfo = new ResultInfo(200, "成功返回！",rtnMap);
                return backRtn(resultInfo,loginId);
            }
        }catch (Exception e){
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(500, "成功返回！",null);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo info(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            Map<String,Object> infoMap = new HashMap<>();

            String id="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            String lx="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if ("1".equals(lx)){
                WxCyxx wxCyxx = new WxCyxx();
                wxCyxx = getBaseMapper().selectById(id);
                infoMap.put("wxCyxx",wxCyxx);
            }
            ResultInfo resultInfo = new ResultInfo(200, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo save(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String lx="",id="",nonce_ms="",nonce_zhi="",xm="",xb="",sfzh="",sjh="";
            String pwd = ToolBean.getMD5Str("111111");
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("xm")) {
                xm = jsonObject.getString("xm");
            }
            if (jsonObject.containsKey("xb")) {
                xb = jsonObject.getString("xb");
            }
            if (jsonObject.containsKey("sfzh")) {
                sfzh = jsonObject.getString("sfzh");
            }
            if (jsonObject.containsKey("sjh")) {
                sjh = jsonObject.getString("sjh");
            }
            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }
            //参数校验
            if (StringUtils.isEmpty(xm) || StringUtils.isEmpty(sfzh) || StringUtils.isEmpty(sjh)){
                return new ResultInfo(500,"关键参数丢失！",null);
            }
            String sfzhjm ="";
            String sjhjm = "";
            try{
                sfzhjm = Url.encrypt(sfzh);
                sjhjm = Url.encrypt(sjh);
            }catch (Exception e){
                return new ResultInfo(500,"手机号或者身份证号加密失败！",null);
            }

            long sl = 0L;
            if ("0".equals(lx)){
                //如果新增，那么要这样判断
                sl= getBaseMapper().selectCount(new QueryWrapper<WxCyxx>().eq("sfzhm",sfzhjm).eq("hyxx_lsh",loginId));
            }else{
                //如果修改，要带上ID，判断
                sl = getBaseMapper().selectCount(new QueryWrapper<WxCyxx>().eq("sfzhm",sfzhjm).eq("hyxx_lsh",loginId).ne("id",id));
            }
            if (sl > 0) {
                return new ResultInfo(500,"成员信息重复！",null);
            }
            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceService.checkNonce(nonce_ms,loginId,nonce_zhi,"成员信息增加操作！");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            WxCyxx wxCyxx = new WxCyxx();

            if ("1".equals(lx)){
                wxCyxx = getBaseMapper().selectById(id);
            }
            wxCyxx.setXm(xm);
            wxCyxx.setXb(xb);
            wxCyxx.setSjhm(sjhjm);
            wxCyxx.setSjhh6w(ToolBean.maskPhone(sjh));
            wxCyxx.setSfzhm(sfzhjm);
            wxCyxx.setSfzh8w(ToolBean.maskIdCard(sfzh));
            ResultInfo resultInfo;
            if ("1".equals(lx)){
                if (!String.valueOf(wxCyxx.getHyxxLsh()).equals(loginId)){
                    return new ResultInfo(500,"非自己维护的成员信息不得修改！",null);
                }
                getBaseMapper().updateById(wxCyxx);
                resultInfo = new ResultInfo(200, "修改成功！",wxCyxx);
            }else{
                wxCyxx.setHyxxLsh(Long.parseLong(loginId));
                getBaseMapper().insert(wxCyxx);
                resultInfo = new ResultInfo(200, "新增成功！",wxCyxx);
            }
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo del(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String ids = "";
            if (jsonObject.containsKey("ids")) {
                ids = jsonObject.getString("ids");
            }
            String [] code = ids.split(",");
            int i=0;
            String str="";

            WxCyxx wxCyxxDel = new WxCyxx();

            for(i=0;i<code.length;i++)
            {
                if (code[i].equals("")) {
                    continue;
                }
                String id = code[i].replaceAll("\\D","").trim();
                wxCyxxDel = getBaseMapper().selectById(id);
                if (!String.valueOf(wxCyxxDel.getHyxxLsh()).equals(loginId)){
                    return new ResultInfo(500,"不能删除非自己维护的用户信息！",null);
                }
                getBaseMapper().deleteById(wxCyxxDel);
            }
            return new ResultInfo(3,"删除成功！",null);
        }
    }
}
